package com.gitee.ice1938.module.admin.app.manage.appversiondetail;

import java.io.IOException;
import java.util.List;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort.Order;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.gitee.ice1938.module.admin.filemanager.fun.FileManageSvc;
import com.gitee.ice1938.util.JsonTool;
import com.gitee.ice1938.util.MD5Util;

import net.dongliu.apk.parser.ByteArrayApkFile;
import net.dongliu.apk.parser.bean.ApkMeta;
import net.dongliu.apk.parser.bean.IconFace;

/**
 * AppVersionDetailServiceImpl
 * 
 * 
 * @author ice1938@qq.com 2019-11-29 14:21:39
 *
 */
@Service
@Transactional
public class AppVersionDetailServiceImpl implements AppVersionDetailService {
	/**
	 * Logger for this class
	 */
	private static final Logger logger = LoggerFactory.getLogger(AppVersionDetailServiceImpl.class);

	@Autowired
	private AppVersionDetailRepository appVersionDetailRepository;

	@Autowired
	private FileManageSvc fileManageSvc;

	@Override
	public Page<AppVersionDetailEntity> findAppVersionDetailData(Map<String, Object> param, Pageable pageable, Order order) {
		// TODO Auto-generated method stub
		return appVersionDetailRepository.findData(param, pageable, order);
	}

	@Override
	public int insertAppVersionDetail(byte[] data, long fileLogastModified, String userLoginName) {
		AppVersionDetailEntity body = new AppVersionDetailEntity();
		try {
			ByteArrayApkFile apkFile = new ByteArrayApkFile(data);
			ApkMeta apkMeta = apkFile.getApkMeta();
			String folderName = apkMeta.getPackageName() + "/" + apkMeta.getLabel() + "/" + apkMeta.getVersionName();
			String fileName = apkMeta.getLabel() + "-" + apkMeta.getVersionName() + ".apk";

			String appPath = fileManageSvc.addFile(folderName, fileName, data, fileLogastModified, true);
			body.setAppMd5(MD5Util.getMd5ByFile(data));
			body.setAppPath(appPath);
			body.setVersionCode(apkMeta.getVersionCode().intValue());
			body.setAppName(apkMeta.getLabel());
			body.setVersionName(apkMeta.getVersionName());
			body.setAppPackage(apkMeta.getPackageName());
			body.setFeature(JsonTool.toJson(apkMeta.getUsesFeatures()));
			body.setMinSdkVersion(Integer.valueOf(apkMeta.getMinSdkVersion()));
			body.setAppSize(data.length);
			String maxSdkVersion = apkMeta.getMaxSdkVersion() != null ? apkMeta.getMaxSdkVersion() : apkMeta
			        .getCompileSdkVersion();
			if (maxSdkVersion != null) {
				body.setTargetSdkVersion(Integer.valueOf(maxSdkVersion));
			}
			List<IconFace> icons = apkFile.getAllIcons();
			if (icons.size() > 0) {
				IconFace icon = icons.get(0);
				appPath = fileManageSvc.addFile(folderName, icon.getPath(), icon.getData(), fileLogastModified, true);
				body.setIconPath(appPath);
			}
			apkFile.close();
			appVersionDetailRepository.insert(body, userLoginName);
		} catch (RuntimeException | IOException e) {
			logger.error("保存出错", e);
			throw new RuntimeException(e);
		}
		return 0;
	}

	@Override
	public int updateAppVersionDetail(AppVersionDetailEntity data, String userLoginName) {
		return appVersionDetailRepository.update(data, userLoginName);
	}

	@Override
	public int disableAppVersionDetail(String appMd5, Boolean disable, String userLoginName) {
		// TODO Auto-generated method stub
		return appVersionDetailRepository.disable(appMd5, disable, userLoginName);
	}

	@Override
	public int deleteAppVersionDetail(String appMd5) {
		// TODO Auto-generated method stub
		return appVersionDetailRepository.delete(appMd5);
	}

	@Override
	public int deleteAppVersionDetailByIds(List<String> ids) {
		return appVersionDetailRepository.delete(ids);
	}

	@Override
	public AppVersionDetailEntity findOne(Integer id) {
		return appVersionDetailRepository.findOne(id);
	}

}
